package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.yunshang.shoppingmall.entity.SysMessage;
import com.yunshang.shoppingmall.service.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*系统消息控制器*/
@Controller
@RequestMapping("/wap")
public class MessageController {

    @Autowired
    SysMessageService sysMessageService;

    //消息推送页面
    @RequestMapping("/getSysMessage")
    public String  getSysMessage(){

        return "shoppingApi/user/message";
    }

    //获取系统消息
    @ResponseBody
    @RequestMapping("/getSysMessageData")
    public Map<String ,Object> getSysMessage(String sSign) {
        Map<String, Object> map = new HashMap<>();
        if ("1".equals(sSign)) {
            List<SysMessage> sysMessageList = sysMessageService.selectSysMessageBySign(sSign);
            map.put("sysMessage", sysMessageList);
            map.put("message", sysMessageList.get(0));
            map.put("messageSize",sysMessageList.size());
            return map;
        }else{
            map.put("message","没有系统消息！");
            return map;
        }

    }

        //获取活动消息
         @ResponseBody
         @RequestMapping("/getActiveMessageData")
        public Map<String, Object> getActiveMessage(String sSign) {
             System.out.println(sSign);
            Map<String, Object> map = new HashMap<>();
            if ("0".equals(sSign)) {
                List<SysMessage> activeMessageList = sysMessageService.selectActiveMessageBySign(sSign);
                map.put("activeMessage", activeMessageList);
                map.put("message", activeMessageList.get(0));
                map.put("messageSize",activeMessageList.size());
                return  map;
            }else{
                map.put("massage","没有活动消息");
                return map;
            }


        }

        /*打开活动列表页*/
        @RequestMapping("/openActiveMessage")
        public ModelAndView openActiveMessage(){
            Map<String ,Object> map =new HashMap<>();
            ModelAndView modelAndView=new ModelAndView("shoppingApi/user/activitymessge");
            List<SysMessage> activeMessageList = sysMessageService.selectActiveMessageBySign("0");
            //记录消息阅读状态
            SysMessage sysMessage=new SysMessage();
            sysMessage.setsSign("0");
            sysMessage.setMessageState("1");
           int row= sysMessageService.updateMessageStateBySign(sysMessage);
            map.put("activeMessageList",activeMessageList);
            modelAndView.addObject("map",map);
            return modelAndView;
        }


    /*打开活动列表页*/
    @RequestMapping("/openSysMessage")
    public ModelAndView openSysMessage(){
        Map<String ,Object> map =new HashMap<>();
        ModelAndView modelAndView=new ModelAndView("shoppingApi/user/activitymessge");
        List<SysMessage> activeMessageList = sysMessageService.selectSysMessageBySign("1");
        //记录消息阅读状态
        SysMessage sysMessage=new SysMessage();
        sysMessage.setsSign("1");
        sysMessage.setMessageState("1");
        int row= sysMessageService.updateMessageStateBySign(sysMessage);
        map.put("activeMessageList",activeMessageList);
        modelAndView.addObject("map",map);
        return modelAndView;
    }

    /*查询所有的消息*/
    @ResponseBody
    @RequestMapping("/getAllMessages")
    public Map<String,Object> getAllMessages(){
        Map<String,Object> map=new HashMap<>();
        List<SysMessage> allMessageList=sysMessageService.selectMessages();
        map.put("allMessageList",allMessageList);
        map.put("allMessageSize",allMessageList.size());
        return map;
    }

    /*查询系统消息未读的条数*/
    @ResponseBody
    @RequestMapping("/selectSysMessageCount")
    public int selectSysMessageCount(String sSign){
        int count=0;
        Integer counts=null;
        counts=sysMessageService.selectMessageCount(sSign);
        if(counts!=null){
            count=counts.intValue();
        }
       return count;
    }

}